import unittest
from unittest.mock import patch

import os
import sys
import logging
import warnings
import threading
import time

path = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
sys.path.insert(0, path)

from contract_market.initialize import config  # NOQA
from contract_market.utils.dqueue import PriorityQueue  # NOQA
from contract_market.initialize import config  # NOQA
from contract_market.initialize.config import *  # NOQA
from contract_market.core.actor import *  # NOQA
from contract_market.core.core import *  # NOQA
from contract_market.server.engine import EventEngine, QuantListener  # NOQA
from contract_market.common.common import EventSystemEngine  # NOQA
from contract_market.common.constant import *  # NOQA
from contract_market.initialize.log import InitLogConfig  # NOQA


def product1(queue):
    count = 0
    for _ in range(100000):
        queue.put(count)


def consumer(queue):
    while 1:
        count = queue.get(count)
        count += 1


def init_engine():
    event = EventCenter()
    engine = EventEngine(event)
    engine.add_engine(QuantListener)
    print(event.obtain_events())
    return event, engine


def on_topic(queue, topic):
    count = 0
    print("监听成功....")
    while 1:
        time.sleep(0.1)
        count += 1
        queue.put(count)


event, engine = init_engine()
queueListener = QuantListener(event, engine)


class TestDqueue(unittest.TestCase):

    @unittest.skip
    def test_init(self):
        pq = PriorityQueue()
        print(pq.get())

        pq.put(1)
        print(pq.get())

        # 写入速度
        product1(pq)

    # @patch("__main__.queueListener.on_topic", new=on_topic)
    @unittest.skip
    def test_engine_queue(self):
        """ 模拟ticker """

        InitLogConfig()
        topic = "1inch_usdt"
        queue = queueListener.on_ticker(topic)

        # time.sleep(1)
        # print(queue.get())
        # queueListener.stop()
        logger = logging.getLogger("simpleExample")

        while 1:
            time.sleep(0.1)
            try:
                logger.debug(queue.get())
            except:
                print("waiting")
                pass

    @unittest.skip
    def test_subscribe_user_balance(self):
        """ """
        InitLogConfig()
        event, engine = init_engine()
        topic = "b1000aa0-9119-4ce7-a963-e7a0fe0055cb"
        # queue = queueListener.on_user_balance(topic)
        queue = engine.on_user_balance(topic)
        logger = logging.getLogger("simpleExample")

        while 1:
            time.sleep(0.1)
            try:
                res = queue.get_notwait()
                logger.debug(res)
            except Exception as e:
                print("waiting", e)
                pass
    
    # @unittest.skip
    def test_subscribe_index_price(self):
        """ """
        InitLogConfig()
        event, engine = init_engine()
        topic = "dot_usdt"
        # queue = queueListener.on_user_balance(topic)
        queue = engine.on_index_price(topic)
        logger = logging.getLogger("simpleExample")

        while 1:
            time.sleep(0.1)
            try:
                res = queue.get_notwait()
                if res:
                    logger.debug(res)
            except Exception as e:
                # print("waiting", e)
                pass

    @unittest.skip
    def test_listen_user_position(self):
        """ """
        InitLogConfig()
        event, engine = init_engine()
        topic = "b1000aa0-9119-4ce7-a963-e7a0fe0055cb"
        # queue = queueListener.on_user_position(topic)
        queue = engine.on_user_position(topic)

        while 1:
            time.sleep(0.1)
            try:
                res = queue.get()
                print("[test_listen_user_order] res:: ", res)
            except Exception as e:
                print("waiting", e)
                pass

    @unittest.skip
    def test_subscribe_user_order(self):
        """ """
        InitLogConfig()
        event, engine = init_engine()
        topic = "b1000aa0-9119-4ce7-a963-e7a0fe0055cb"
        # queue = queueListener.on_user_trade(topic)
        queue = engine.on_user_trade(topic)
        logger = logging.getLogger("simpleExample")

        while 1:
            time.sleep(0.1)
            try:
                res = queue.get()
                logger.debug(res)
            except Exception as e:
                print("waiting", e)
                pass

    @unittest.skip
    def test_subscribe_ticker(self):
        InitLogConfig()
        topic = "1inch_usdt"
        queue = queueListener.on_ticker(topic)
        logger = logging.getLogger("simpleExample")

        while 1:
            time.sleep(0.1)
            try:
                res = queue.get()
                print("[res] >>> ", res)
                # logger.debug(res)
            except Exception as e:
                print("waiting", e)
                pass
        pass
    
    @unittest.skip
    def test_focus_listen_ticker(self):
        event, engine = init_engine()
        InitLogConfig()
        topic = "dot_usdt"
        # queue = queueListener.on_ticker(topic)
        queue = engine.on_ticker(topic)
        logger = logging.getLogger("simpleExample")

        while 1:
            time.sleep(0.1)
            print('sleep')
            try:
                res = queue.get()
                print(res)
            except Exception as e:
                print("waiting", e)
                pass


if __name__ == "__main__":
    unittest.main()
    pass
